#coding:utf-8
import requests
from pyquery import PyQuery as pq
'''
知乎发现页面信息存储为txt
'''
url = 'https://www.zhihu.com/explore'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
#使用requests进行请求的发送：可以设置请求头
html = requests.get(url, headers=headers).text
#得到的数据进行pyquery的初始化
doc = pq(html)
items = doc('.explore-tab .feed-item').items()
#组装信息结构并储存
for item in items:
    question = item.find('h2').text()
    author = item.find('.author-link').text()
    answer = pq(item.find('textarea.content').text()).text()
    #写入文件
    with open('zhihu.txt', 'a', encoding='utf-8') as f:
        f.write('\n'.join([question, author, answer]))
        f.write('\n' + '='*50 + '\n')